package com.ddwl.wallet.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.UserEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.wallet.OldSalesRankBo;
import com.ddwl.schema.bo.wallet.QueryWalletReportBo;
import com.ddwl.schema.bo.wallet.WalletFlowQueryBo;
import com.ddwl.schema.bo.wallet.WalletFlowStatisticsQueryBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.wallet.*;
import com.ddwl.wallet.dao.dto.WalletFlowStatisticsDto;
import com.ddwl.wallet.service.DistOrderService;
import com.ddwl.wallet.service.UserWalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;


/**
 * 类UserWalletController.java的实现描述：用户钱包相关接口
 *
 * @author 27670
 * @Date : 2020-07-21 13:56.
 */
@RequestMapping("user")
@Api(tags = "用户钱包相关接口")
@RestController
public class UserWalletController extends BaseController {

    @Resource
    private UserWalletService userWalletService;
    @Resource
    private DistOrderService distOrderService;

    @ApiOperation("五八马用户查询钱包数据")
    @GetMapping("/wbm/money")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<UserWalletMainReportVo> getShopUserWallet(){
        return RestVo.ok(userWalletService.wbmShopWallet(getUserId()));
    }

    @ApiOperation("公益账户钱包数据")
    @GetMapping("/charity/money")
    @Auth(required = false)
    public RestVo<UserWalletMainReportVo> getCharityWallet(){
        return RestVo.ok(userWalletService.wbmShopWallet(UserEnum.AccountEnum.CHARITY_ACCOUNT.getId()));
    }

    @ApiOperation("查询用户钱包含预估收入")
    @GetMapping("/money")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo<UserWalletVo> getUserWallet(){
        return RestVo.ok(userWalletService.getUserWalletEstimate(getUserId()));
    }

    @ApiOperation("查询用户钱包变更流水报表")
    @PostMapping("/money/flowReport")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<WalletFlowStatisticsDto>> queryMoneyFlowReport(@RequestBody PageBo<WalletFlowStatisticsQueryBo> bo){
        if(bo.getParam() ==null){
            bo.setParam(new WalletFlowStatisticsQueryBo());
        }
        bo.getParam().setUserId(getUserId());
        return RestVo.ok(userWalletService.queryMoneyFlowReport(bo));
    }

    @ApiOperation("查询用户钱包变更流水")
    @PostMapping("/money/flow")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo<PageVo<UserWalletFlowVo>> queryMoneyFlow(@RequestBody PageBo<WalletFlowQueryBo> bo){
        if(bo.getParam() ==null){
            bo.setParam(new WalletFlowQueryBo());
        }
        bo.getParam().setUserId(getUserId());
        return RestVo.ok(userWalletService.queryMoneyFlow(bo));
    }


    @ApiOperation("查询收益")
    @PostMapping("/walletReport")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<List<UserWalletReportVo>> getWalletReport(@RequestBody QueryWalletReportBo bo){
        return RestVo.ok(distOrderService.getUserPreIncome(bo,getUserId()));
    }

    @ApiOperation("总收益")
    @PostMapping("/walletMainReport")
    @Auth(product = {BaseEnum.ProductEnum.MALL,BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.FENXIAO})
    public RestVo<UserWalletMainReportVo> walletMainReport(){
        return RestVo.ok(distOrderService.walletMainReport(getUserId()));
    }

    @PostMapping("/createOldSalesRank")
    @ApiOperation("创建或修改排行榜")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<String> createAndUpdateOldSalesRank(@Valid @RequestBody OldSalesRankBo bo) {
        return RestVo.ok(userWalletService.createOldSalesRank(bo));
    }

    @PostMapping("/queryOldSalesRank")
    @ApiOperation("查询排行榜")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<OldSalesRankVo>> queryJoinerList(@Valid @RequestBody PageBo<OldSalesRankBo> pageBo) {
        return RestVo.ok(userWalletService.oldSalesRankList(pageBo));
    }

    @PostMapping("/correctWalletInfo")
    @ApiOperation("订正分销用户钱包数据")
    @Auth(product = {BaseEnum.ProductEnum.API})
    RestVo correctWalletInfo(@RequestParam("fxUserId") String fxUserId, @RequestParam("userId")String userId){
        userWalletService.correctWalletInfo(fxUserId, userId);
        return RestVo.ok();
    }

    @GetMapping("/changeWallet")
    @ApiOperation("用户钱包变更")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo changeWallet(@RequestParam String changeRecord, @RequestParam String userId, @RequestParam Integer walletFlowType, @RequestParam BigDecimal amount){
        userWalletService.changeUserWallet(changeRecord,userId,walletFlowType,amount);
        return RestVo.ok();
    }

}














